home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 42 / Amiga Format AFCD42 (Issue 126, Aug 1999).iso / -serious- / hardware / blizkick / modules / patchmath020.asm < prev    next >
Assembly Source File  |  1999-05-17  |  17KB  |  813 lines

  1. ; FILE: Source:modules/PatchMath020.ASM          REV: 1 --- Patches C-compiler math routines with 020+ code
  2.  
  3. ;
  4. ; PatchMath020 module for BlizKick
  5. ; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  6. ; This module replaces C-compiler math routines with 020+
  7. ; opcodes. V1.1 enables non-64bit mul/div patches for 060.
  8. ; Also sped-up patch scanning a lot.
  9. ;
  10. ; Written by Harry Sintonen.
  11. ; This source code is Public Domain.
  12. ;
  13. ; NOTE: Optimization routine was sent to me by Dave Jones.
  14. ; Thanks a lot Dave!
  15. ;
  16. ;
  17.  
  18.     incdir    "include:"
  19.     include    "exec/execbase.i"
  20.     include    "exec/libraries.i"    ; Some required...
  21.     include    "exec/exec_lib.i"
  22.     include    "blizkickmodule.i"
  23.  
  24.     SECTION    PATCH,CODE
  25. _DUMMY_LABEL
  26.     BK_PTC
  27.  
  28. ; Code is run with following incoming parameters:
  29. ;
  30. ; a0=ptr to ROM start (buffer)    eg. $1DE087B8
  31. ; a1=ptr to ROM start (ROM)    eg. $00F80000 (do *not* access!)
  32. ; d0=ROM lenght in bytes    eg. $00080000
  33. ; a2=ptr to _FindResident routine (will search ROM buffer for resident tag):
  34. ;    CALL: jsr (a2)
  35. ;      IN: a0=ptr to ROM, d0=rom len, a1=ptr to resident name
  36. ;     OUT: d0=ptr to resident (buf) or NULL
  37. ; a3=ptr to _InstallModule routine (can be used to plant a "module"):
  38. ;    CALL: jsr (a3)
  39. ;      IN: a0=ptr to ROM, d0=rom len, a1=ptr to module, d6=dosbase
  40. ;     OUT: d0=success
  41. ; a4=ptr to _Printf routine (will dump some silly things (errormsg?) to stdout ;-)
  42. ;    CALL: jsr (a4)
  43. ;      IN: a0=FmtString, a1=Array (may be 0), d6=dosbase
  44. ;     OUT: -
  45. ; d6=dosbase, a6=execbase
  46. ;
  47. ; Code should return:
  48. ;
  49. ; d0=true if succeeded, false if failed.
  50. ; d1-d7/a0-a6 can be trashed. a7 *must* be preserved! ;-)
  51.  
  52. LTSFVARSTACK    EQU    8
  53.     STRUCTURE KTSTR,0
  54.     APTR    KT_LoadAddr    ; scan start
  55.     ULONG    KT_LoadFileSize    ; scan lenght
  56.     APTR    KT_CallBack    ; null or ptr to printf func
  57.     STRUCT    KT_PrintFVarStack,LTSFVARSTACK
  58.     LABEL    KTSTR_SIZEOF
  59.  
  60. Push    MACRO
  61.     move.l    \1,-(sp)
  62.     ENDM
  63. Pop    MACRO
  64.     move.l    (sp)+,\1
  65.     ENDM
  66. Pushm    MACRO
  67.     movem.l    \1,-(sp)
  68.     ENDM
  69. Popm    MACRO
  70.     movem.l    (sp)+,\1
  71.     ENDM
  72. Call    MACRO
  73.     jsr    (_LVO\1,a6)
  74.     ENDM
  75.  
  76.     btst    #AFB_68020,(AttnFlags+1,a6) ; need minimum 020
  77.     bne.b    .cont
  78. .fail    moveq    #0,d0
  79.     rts
  80.  
  81. .cont
  82.     tst.b    (AttnFlags+1,a6)    ; If no 060 enable all patches:
  83.     bpl.b    .noenall
  84.     lea    (C_Opt1,pc),a5
  85. .enableall    tst.l    (a5)
  86.     beq.b    .noenall
  87.     st    (a5)            ; opt_enable
  88.     lea    (OptimStructSize,a5),a5
  89.     bra.b    .enableall
  90. .noenall
  91.     move.l    a4,_printf        ; Init callback hook
  92.     move.l    d6,_d6reg
  93.  
  94.     lea    (ktstr,pc),a4        ; Build KTSTR structure
  95.     move.l    a0,(KT_LoadAddr,a4)
  96.     move.l    d0,(KT_LoadFileSize,a4)
  97.     lea    (_callback,pc),a0
  98.     move.l    a0,(KT_CallBack,a4)
  99.  
  100.     ; Patch_CompilerRoutines
  101.     ;  IN: a1=KTSTR
  102.     ; OUT: -
  103.     move.l    a4,a1            ; ...and go
  104.     bsr    Patch_CompilerRoutines
  105.  
  106.     moveq    #1,d0            ; Set to 0 for debug
  107.     rts
  108.  
  109. ;  IN: a0=string to print (no linefeed!), a4=KTSTR
  110. ; OUT: -
  111. ;NOTE: may trash a0-a4/a6 and d0-d1 (at least)
  112. _printf    dc.l    0
  113. _d6reg    dc.l    0
  114. _callback    move.l    d6,-(sp)
  115.     move.l    (_d6reg,pc),d6        ; get orig d6 (DosBase)
  116.     lea    (KT_PrintFVarStack,a4),a1
  117.     move.l    (_printf,pc),a2        ; get printf
  118.     jsr    (a2)            ; print it
  119.     lea    (.lf,pc),a0        ; print linefeed
  120.     jsr    (a2)
  121.     move.l    (sp)+,d6
  122.     rts
  123. .lf    dc.b    10,0
  124.  
  125.     CNOP    0,2
  126. ktstr    ds.b    KTSTR_SIZEOF
  127.  
  128.  
  129.     OPT    O-            ; Turn off all optimizations
  130.  
  131. * Dave Jones:
  132. *
  133. * " Finally,  here's  a routine from KickTool (Now inactive) that you
  134. *   may  like  to  add  to  BlizKick.   It  replaces  C compiler Math
  135. *   routines  with  68020  opcodes.   It  probably will need a little
  136. *   hacking  to  get it to work with BlizKick, but I'm sure your more
  137. *   than capable of doing this..
  138. *
  139. *   Note also, that this is a generic routine I wrote for all-purpose
  140. *   exe files, but not all these compiler routines are in kickstarts,
  141. *   so  you  may remove some of them.  I guess you've got quite a few
  142. *   kicks that you can test on, to decide which ones need to remain. "
  143.  
  144.  
  145. *#############################################################################
  146. *#######
  147. *####### ROUTINES BY DAVE JONES, MODIFIED BY HS
  148. *#######
  149. *####### Note that it would be nice to ask Dave Jones for the permission and
  150. *####### the original routines if you intend to use these. This mostly because
  151. *####### I can't be sure if I haven't messed up things again... ;)
  152. *#######
  153.  
  154. *#############################################################################
  155.  
  156. *HS:
  157. * Made this thing run faster.
  158.  
  159.  STRUCTURE optim,0
  160.    UWORD opt_enable
  161.    UWORD opt_offset
  162.    UWORD opt_size
  163.    UWORD opt_repsize
  164.    UWORD opt_repoffset
  165.    UWORD opt_txtoffset
  166.    LABEL OptimStructSize
  167.  
  168. Patch_CompilerRoutines
  169.     Pushm    d0-d2/a0-a5
  170.     Move.l    a1,a4
  171.     Move.l    KT_LoadAddr(a4),a0
  172.  
  173.     move.l    KT_LoadFileSize(a4),d2
  174.     add.l    a0,d2
  175.  
  176. ;******************************************************************
  177.  
  178.     lea    C_Opt1(PC),a2
  179. .SpotOptimisation
  180.     tst.w    (a2)            ;opt_enable
  181.     beq.b    .not_enabled
  182.  
  183.     move.w    opt_offset(a2),a1    ; Get ptr to original C to find
  184.     add.l    a2,a1
  185. .HuntLoop
  186.     move.l    a1,a5
  187.     ; Find
  188.     move.w    opt_size(a2),d1        ; size of original C
  189.     move.l    a0,a3
  190.     subq    #1,d1            ; Correct for the dbcc
  191. .seek    cmpm.w    (a5)+,(a3)+
  192.     dbne    d1,.seek
  193.     bne.b    .NotFound
  194.  
  195.     bsr.b    .FoundOptim
  196.  
  197. .NotFound    addq.l    #2,a0
  198.  
  199.     cmp.l    a0,d2
  200.     bgt.b    .HuntLoop        ; Go back
  201.  
  202. ;******************************************************************
  203. ; If we get here, then we've scanned to EOF for the current
  204. ; optimisation, so move back to beginning of file, and
  205. ; try a different optimisation.
  206.  
  207.     Move.l    KT_LoadAddr(a4),a0    ; restore a0
  208.  
  209. .not_enabled    Lea    OptimStructSize(a2),a2
  210.     Tst.l    (a2)            ; get next optimisation
  211.     Bne.b    .SpotOptimisation
  212.  
  213. .NoMorePatches    Popm    d0-d2/a0-a5
  214.     Rts
  215.  
  216. ;-------------------------------------------------------------------
  217.  
  218.     CNOP    0,4
  219. .FoundOptim    Tst.l    KT_CallBack(a4)
  220.     Beq.b    .QuietMode
  221.  
  222.     Moveq.l    #0,d1
  223.     Move.w    opt_txtoffset(a2),d1
  224.     Add.l    a2,d1
  225.     Move.l    d1,KT_PrintFVarStack(a4) ; Name
  226.  
  227.     Pushm    a0-a4/a6
  228.     Sub.l    KT_LoadAddr(a4),a0
  229.     Move.l    a0,KT_PrintFVarStack+4(a4)
  230.  
  231.     Lea    FoundTxt(pc),a0        ; body
  232.     Move.l    KT_CallBack(a4),a2
  233.     Jsr    (a2)
  234.     Popm    a0-a4/a6
  235.  
  236. .QuietMode
  237.     Move.w    opt_repsize(a2),d0    ; size of my version
  238.     Subq.l    #1,d0            ; correct for dbf
  239.  
  240.     Move.w    opt_repoffset(a2),a1    ; addr of rep code
  241.     Add.l    a2,a1
  242. .CopyPatch    Move.w    (a1)+,(a0)+
  243.     dbf    d0,.CopyPatch
  244.  
  245.  
  246.     Move.w    opt_size(a2),d0        ; original C size in words
  247.     Sub.w    opt_repsize(a2),d0    ; Mycode size in words
  248.     Subq.l    #1,d0
  249.  
  250. .ClrLoop    Clr.w    (a0)+
  251.     dbf    d0,.ClrLoop
  252.     Rts
  253.  
  254. FoundTxt    Dc.b    "Patched %s routine at offset $%lx",0
  255.     CNOP    0,2    
  256.  
  257. ;-------------------------------------------------------------------
  258. *HS:
  259. * Added parameter \2 that tells whether this patch should be done on
  260. * 060.
  261.  
  262. Optim    Macro
  263. .List\1    Dc.w    \2
  264.     Dc.w    \1-.List\1
  265.     Dc.w    \1Size/2
  266.     Dc.w    \1RepSize/2
  267.     Dc.w    \1Rep-.List\1
  268.     Dc.w    \1Txt-.List\1
  269. \1DEF    SET    1
  270.     Endm
  271.  
  272. *HS:
  273. * Here you can enable different optimizations. Only DiceC Mulu and Divs
  274. * can be found from current ROM images.
  275. *
  276.  
  277. C_Opt1    Optim    DiceC_Mulu,1
  278.     Optim    DiceC_Divs,0
  279.     ;Optim    DiceC_MovMem,1
  280.     ;Optim    DiceC_StackLongMuls,1
  281.     ;Optim    Manx_Divs,1
  282.     ;Optim    Manx_Mulu,1
  283.     ;Optim    Manx_Mods,0
  284.     ;Optim    Manx_DivuModu,0
  285.     ;Optim    Manx_DivuModu2,0
  286.     ;Optim    SASMuls,1
  287.     ;Optim    SASMulu,1
  288.     ;Optim    SASDivsMods,0
  289.     ;Optim    SASDivuModu,0
  290.     ;Optim    SASDivsL,0
  291.     ;Optim    GenericMul1,0
  292.     dc.l    0            ** END MARKER!
  293.  
  294. ***************************************************
  295.     IFD    SASMulsDEF
  296. SASMuls
  297. __H0_end    movem.l    D1-D4,-(SP)
  298.     move.l    D0,D4
  299.     eor.l    D1,D4
  300.     tst.l    D0
  301.     beq.b    lbC001C9C
  302.  
  303.     bpl.b    lbC001C70
  304.  
  305.     neg.l    D0
  306. lbC001C70    move.l    D0,D2
  307.     tst.l    D1
  308.     bne.b    lbC001C7A
  309.  
  310.     clr.l    D0
  311.     bra.b    lbC001C9C
  312.  
  313. lbC001C7A    bpl.b    lbC001C7E
  314.  
  315.     neg.l    D1
  316. lbC001C7E    move.l    D0,D3
  317.     mulu    D1,D3
  318.     swap    D2
  319.     mulu    D1,D2
  320.     swap    D2
  321.     clr.w    D2
  322.     add.l    D2,D3
  323.     swap    D1
  324.     mulu    D1,D0
  325.     swap    D0
  326.     clr.w    D0
  327.     add.l    D3,D0
  328.     tst.l    D4
  329.     bpl.b    lbC001C9C
  330.  
  331.     neg.l    D0
  332. lbC001C9C    movem.l    (SP)+,D1-D4
  333.     rts
  334. SASMulsSize    = *-SASMuls
  335.  
  336. ;==================================================
  337. SASMulsRep
  338. _rep0    muls.l    D1,D0
  339.     rts
  340. SASMulsRepSize    = *-SASMulsRep
  341. SASMulsTxt        Dc.b    "SAS C Muls",0
  342.     CNOP    0,2
  343.     ENDC
  344. ***************************************************
  345.     IFD    SASMuluDEF
  346. SASMulu
  347. _seq1    movem.l    D1-D3,-(SP)
  348.     move.l    D0,D2
  349.     beq.b    lbC001CD0
  350.  
  351.     tst.l    D1
  352.     bne.b    lbC001CB8
  353.  
  354.     clr.l    D0
  355.     bra.b    lbC001CD0
  356.  
  357. lbC001CB8    move.l    D0,D3
  358.     mulu    D1,D3
  359.     swap    D2
  360.     mulu    D1,D2
  361.     swap    D2
  362.     clr.w    D2
  363.     add.l    D2,D3
  364.     swap    D1
  365.     mulu    D1,D0
  366.     swap    D0
  367.     clr.w    D0
  368.     add.l    D3,D0
  369. lbC001CD0    movem.l    (SP)+,D1-D3
  370.     rts
  371. SASMuluSize    = *-SASMulu
  372.  
  373. ;==================================================
  374. SASMuluRep
  375. _rep1    mulu.l    D1,D0
  376.     rts
  377. SASMuluRepSize